﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
namespace Time.DB
{
    /// <summary>
    /// OleDB连接方式操作Access数据库的处理
    /// </summary>
    public class DBConnectHelp
    {
        #region 通过业务类型配置信息，组建数据连接字符串
        /// <summary>
        /// 获取数据连接表的数据 
        /// </summary>
        /// <param name="objdbLink">连接业务类型对象 </param>
        public static  string getConStr(  t_dbLink objdbLink)
        {
            string conStr = null;
            //  SqlHelp objSqlHelp = new SqlHelp();
            try
            {
                if (objdbLink.DBAddress.Contains("~\\"))
                {
                    objdbLink.DBAddress = System.Environment.CurrentDirectory + objdbLink.DBAddress.Replace("~", "");
                }
                if (objdbLink.DBType.ToUpper().Equals("ORACLE"))
                {
                    conStr = string.Format(@"Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST={0}) (PORT=1521)))(CONNECT_DATA=(SERVICE_NAME= {1})));User Id={2}; Password={3}", objdbLink.DBAddress, objdbLink.DBName, objdbLink.DBUser, objdbLink.DBPwd);
                }
                else if (objdbLink.DBType.ToUpper().Equals("SQLSERVER"))
                {

                    conStr = string.Format(@"Data Source = {0};Initial Catalog = {1};User Id = {2};Password = {3};", objdbLink.DBAddress, objdbLink.DBName, objdbLink.DBUser, objdbLink.DBPwd);
                }
                else if (objdbLink.DBType.ToUpper().Equals("ACCESS"))
                {
                    if (objdbLink.DBAddress.ToUpper().Contains("MDB"))
                    {
                        if (string.IsNullOrEmpty(objdbLink.DBPwd))
                        {
                           
                             conStr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False", objdbLink.DBAddress);
                        }
                        else
                        { 
                            conStr = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}; Jet OLEDB:Database Password={1};Persist Security Info=False", objdbLink.DBAddress, objdbLink.DBPwd);
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(objdbLink.DBPwd))
                        {
                            conStr = conStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0};Persist Security Info=False", objdbLink.DBAddress);
                        }
                        else
                        {
                            conStr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Jet OLEDB:Database Password={1};Persist Security Info=False", objdbLink.DBAddress, objdbLink.DBPwd);

                        }
                    }
                }
                else if (objdbLink.DBType.ToUpper().Equals("MYSQL"))
                {
                    conStr = string.Format(@"server={0};User Id={1};password={2};Database={3}", objdbLink.DBAddress, objdbLink.DBUser, objdbLink.DBPwd, objdbLink.DBName);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("数据连接配置获取失败，详细：" + ex.Message);
            }
             
            return conStr;
        }
        #endregion
    }

}
